package com.wangx1n.dao.impl;

import com.wangx1n.dao.UserDao;
import com.wangx1n.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangXin
 * @date 2020/12/4 15:10
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addUser(User user) {
        String sql = "insert into user values(?,?)";
        //第二个参数是可变参数
        jdbcTemplate.update(sql, user.getUserId(), user.getUserName());
    }

    @Override
    public void updateUser(User user) {
        String sql = "update user set  user_name=? where user_id = ?;";
        //第二个参数是可变参数
        jdbcTemplate.update(sql, user.getUserName(), user.getUserId());
    }

    @Override
    public void deleteUser(User user) {
        String sql = "delete from user where user_id = ?;";
        //第二个参数是可变参数
        jdbcTemplate.update(sql, user.getUserId());
    }

    @Override
    public int selectUserCount() {
        String sql = "select count(*) from user";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public User selectUser(Integer userId) {
        String sql = "select * from user where user_id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), userId);
    }

    @Override
    public List<User> selectUsers() {
        String sql = "select * from user";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public void batchAddUser(List <Object[]> batchArgs){
        String sql = "insert into user values(?,?)";
        jdbcTemplate.batchUpdate(sql,batchArgs);
    }

    @Override
    public void batchUpdateUser(List <Object[]> batchArgs){
        String sql = "update user set  user_name=? where user_id = ?;";
        jdbcTemplate.batchUpdate(sql,batchArgs);
    }

    @Override
    public void batchDeleteUser(List <Object[]> batchArgs){
        String sql = "delete from user where user_id = ?;";
        jdbcTemplate.batchUpdate(sql,batchArgs);
    }




}
